package com.daifeng.gulimall.order.Interceptor;

import com.daifeng.common.constant.CartConstant;
import com.daifeng.common.constant.UserConstant;
import com.daifeng.common.vo.MemberResponseVO;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * 执行目标方法之前，判断用户的登录状态。并封装传递给目标请求
 *
 */
public class OrderInterceptor implements HandlerInterceptor {


    //ThreadLocal同一个线程共享数据
     public static ThreadLocal<MemberResponseVO> threadLocal = new ThreadLocal<>();


    /**
     * 在目标方法执行之前拦截
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        MemberResponseVO member = (MemberResponseVO) session.getAttribute(UserConstant.LOGIN_USER);
        /**
         * 订单服务的前提是用户进行登录 所以拦截订单服务的所有请求来判断是否登录
         */
        if(member != null){
            threadLocal.set(member);
            return true;
        }else {
            session.setAttribute("msg","请先进行登录！！！");
            response.sendRedirect("http://auth.gulimall.com/login.html");
            return false;
        }
    }
}
